package com.fyzk.activity.user.userinfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.BankListResponse;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.List;


public class PersonalInforBankAdapter extends BaseAdapter{

	private Context context;
	private List<BankListResponse> bankInfos = new ArrayList<BankListResponse>();
	private ViewHolder holder = null;
	private DeleteBankListener deleteListener;

	public PersonalInforBankAdapter(Context context, List<BankListResponse> bankInfos, DeleteBankListener deleteListener){
		this.context = context;
		this.bankInfos = bankInfos;
		this.deleteListener = deleteListener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (bankInfos != null) {
			return bankInfos.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return  bankInfos.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		BankListResponse info = bankInfos.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.user_bankinfor_item, null);
			holder.num_tv = (TextView) convertView.findViewById(R.id.bank_num);
			holder.name_tv = (TextView) convertView.findViewById(R.id.bank_name);
			holder.weiHao_tv = (TextView) convertView.findViewById(R.id.bank_weihao);
			holder.show_ll = (LinearLayout) convertView.findViewById(R.id.bank_show_ll);
			holder.more_ll = (LinearLayout) convertView.findViewById(R.id.bank_more_ll);
			holder.userName_tv = (TextView) convertView.findViewById(R.id.bank_username);
			holder.province_tv = (TextView) convertView.findViewById(R.id.bank_province);
			holder.city_tv = (TextView) convertView.findViewById(R.id.bank_city);
			holder.branch_tv = (TextView) convertView.findViewById(R.id.bank_branck);

			holder.deletBtn = (Button) convertView.findViewById(R.id.bank_delete);
			holder.bank_detail_ll = (LinearLayout) convertView.findViewById(R.id.bank_bank_ll);
			holder.alipay_detail_ll = (LinearLayout) convertView.findViewById(R.id.bank_a_ll);
			holder.alipay_userName_tv = (TextView) convertView.findViewById(R.id.alipay_username);

			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		holder.num_tv.setText((position+1) + "");
		if (info.getDrawType() == 2) {
			holder.name_tv.setText("类型：" + info.getBankName());
			holder.weiHao_tv.setText(PublicMethod.tv_yinCang(info.getBankCard(), 3, 3));
			holder.bank_detail_ll.setVisibility(View.GONE);
			holder.alipay_detail_ll.setVisibility(View.GONE);
		}else{
			holder.name_tv.setText("类型：" + info.getBankName());
			holder.weiHao_tv.setText(PublicMethod.tv_yinCang(info.getBankCard(), 3, 3));
			holder.bank_detail_ll.setVisibility(View.GONE);
			holder.alipay_detail_ll.setVisibility(View.GONE);
		}


		holder.deletBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (deleteListener != null){
					deleteListener.deleteBank(position);
				}
			}
		});

		return convertView;
	}

	public void setList(List<BankListResponse> bankInfos){
		this.bankInfos = bankInfos;
		notifyDataSetChanged();
	}

	private  class ViewHolder{
		TextView num_tv;
		TextView name_tv;
		TextView weiHao_tv;
		TextView userName_tv;
		TextView province_tv;
		TextView city_tv;
		TextView branch_tv;
		LinearLayout more_ll;
		LinearLayout show_ll;
		LinearLayout bank_detail_ll;    // 银行卡详细
		LinearLayout alipay_detail_ll;  // 支付宝详细
		TextView alipay_userName_tv;    // 支付宝持卡人
		Button deletBtn;
	}

	interface DeleteBankListener{
		void deleteBank(int id);
	}

}

